<?php
class Object extends XMap {
    private  $itemFileName = "objects";
    public $stopPlayer = "stopPlayer";
    
    public function __construct($configFile = null, $xmlMap = null) {
        parent::__construct($configFile, $xmlMap);
        $this->load("sound");
    }

    //Creates different objects on the scene
    public function createObjects() {
        $objects = $this->xmlMap->objects->children();
        if(!empty($objects)) {
            foreach($objects as $object) {
                $type = $object->attributes()->type;
                if($type=='img') {
                    $ret[] = $this->createImage($object);
                } else {
                    $ret[] = $this->createObject($object);
                }
            }
            if(is_array($ret) && !empty($ret)) {
                return $ret;
            }
        }
        return false;
    }
    
    //Creates objects on the screen
    private function createObject($object) {
        $object = $object->attributes();
        $objectType = explode("/", $object[0]);
        $configObj = $this->findNode($this->itemFile[$this->itemFileName]->children(), $objectType);
        
        //Stops the script if node is not configured
        if(!$configObj) {
            $file = $this->itemFile[$this->itemFileName];
            $this->error("Object Type '$object' is not defined in objects..xml!");
            return false;
        } else {
            $configObj = $configObj->attributes();
        }
        
        if($object->stopPlayer || $configObj->stopPlayer) {
            $stopPlayer = $this->stopPlayer;
        } else {
            $stopPlayer = "";
        }
        
        if(!empty($stopPlayer) || !empty($object->hitSound) || !empty($configObj->hitSound) || !empty($object->surfaceSound) || !empty($configObj->surfaceSound)) {
            $col = $this->colObject;
        } else {
            $col = "";
        }
        
        //Object definitions
        $width = (strlen($object->width) > 0) ? "width:". $object->width. "px;" : "";
        $height =(strlen($object->height) > 0) ? "height:". $object->height. "px;" : "";
        $layer = (strlen($object->layer) > 0) ? "z-index:". $object->layer. ";" :  "";
        $posX = (strlen($object->posX) > 0) ? str_replace(" ", "","left:".  $object->posX. "px;") :"" ;
        $posY = (strlen($object->posY) > 0) ? str_replace(" ", "", "top:". $object->posY. "px;") :"" ;
        $bgColor = (strlen($object->bgColor) > 0) ? "background-color: ". $configObj->bgColor : "";
        $trigger = (strlen($object->trigger) > 0) ? $object->trigger : "";
        $border = (strlen($object->border) > 0) ? $object->border : "";
        $repeat = (strlen($configObj->repeat) > 0) ? $configObj->repeat : "";
        $id = $this->getUniID('object');    
        
        if(!empty($configObj->src)) {
            $bgImg = $this->folders['objectFolder']. $configObj->src;
            $bgImg = "background: url($bgImg) $repeat;";
        } else {
            $bgImg = "";
        }
        
        //Object sound
        $sound = "hasSound";
        if(isset($object->hitSound) && !empty($object->hitSound)) {
            $this->sound->loadSoundFromObject($object->hitSound, $id);
        } elseif(isset($configObj->hitSound) && !empty($configObj->hitSound)) {
            $this->sound->loadSoundFromObject($configObj->hitSound, $id);
        } else {
            $sound = "";
        }
        
        //Surface sound
        $surfaceSound = "hasSurfaceSound";
        if(isset($object->surfaceSound) && !empty($object->surfaceSound)) {
            $this->sound->loadSoundFromObject($object->surfaceSound, $id, true);
        } elseif(isset($configObj->surfaceSound) && !empty($configObj->surfaceSound)) {
            $this->sound->loadSoundFromObject($configObj->surfaceSound, $id, true);
        } else {
            $surfaceSound = "";
        }
        
        //Checks to see if a trigger is defined
        if(!empty($trigger)) {
            $trigger = "trigger $trigger";
        } else {
            $trigger = "";
        }
        
        //If no width and height is defined, XMAP default config is used
        if(empty($width) && empty($height)) {
            $width = $configObj->width;
            $height = $configObj->height;
        }
        
        //Checks to see if border is enabled
        if($border == "true") {
            $cssBorder = "border: 1px solid black;";
        } else {
            $cssBorder = "";
        }
        
        //Adding PX
        $width = str_replace(" ","", $width);
        $height =  str_replace(" ","", $height);
        
        //Repeat    
        $html = "<div id='$id' class='$object $col $stopPlayer $trigger $sound $surfaceSound' style='display: block; position: absolute; $posX$posY$width$height$layer$bgColor$cssBorder $bgImg'></div>";
        return $html;
    }
    
    //Creates a image on the scene
    private function createImage($imgObject) {
        $src = $this->folders['imgFolder']. $imgObject->attributes()->src;
        if(!file_exists($src)) {
            $width = $imgObject->attributes()->width;
            $height = $imgObject->attributes()->height;
            $zIndex = $imgObject->attributes()->layer;
            $collide = $imgObject->attributes()->collide;
            $posX = $imgObject->attributes()->posX. "px";
            $posY = $imgObject->attributes()->posY. "px";
            
            if($collide == "true") {
                $col = $this->colObject;
            } else {
                $col = "";
            }
            
            $html = "<img src='$src' width='$width' height='$height' style='z-index: $zIndex; left: $posX; top: $posY; position: absolute;' class='sceneImg $col'/>";
            return $html;
        } else {
            $this->error($src. " does not exist!");
        }
       
        return false;
    }
}

?>
